package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池配置类
 * @Configuration 标记为配置类
 * @ComponentScan 扫描指定包下的组件（确保包含TimePrintTask所在的包）
 */
@Configuration
@ComponentScan("com.example.task")
public class ThreadPoolConfig {

    /**
     * 定义线程池Bean，供IOC容器管理
     */
    @Bean(name = "taskExecutor")
    public ThreadPoolExecutor taskExecutor() {
        // 核心线程数
        int corePoolSize = 2;
        // 最大线程数
        int maximumPoolSize = 5;
        // 空闲线程存活时间
        long keepAliveTime = 60;
        // 时间单位
        TimeUnit unit = TimeUnit.SECONDS;
        // 任务队列
        ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10);

        return new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue,
                new ThreadPoolExecutor.AbortPolicy()
        );
    }
}
